#!/bin/sh

#AUM/val
#AUM/tes
#AUS/val
#AUS/tes
#AIS/val
#AIS/tes
#AIM/tes

#Nice K: 
#5	200198
#10	100099
#31	32290
#62	16145
#155	6458
#310	3229

#Default values
start=0
stop=1000990
nrnodes=62
time="00:15:00"
k=310
algorithm="AUM"
dataset="val"
treads=20

#Read command line arguments
args=("$@")
argslen=$#

for (( i=0;i<$argslen;i++)); do
	if [ ${args[${i}]} = "-a" ] ; then
		i=$((i+1))
		algorithm=${args[${i}]}
	elif [ ${args[${i}]} = "-t" ] ; then
		i=$((i+1))
		time=${args[${i}]}

	elif [ ${args[${i}]} = "-n" ] ; then
		i=$((i+1))
		nrnodes=${args[${i}]}

	elif [ ${args[${i}]} = "-k" ] ; then
		i=$((i+1))
		k=${args[${i}]}

	elif [ ${args[${i}]} = "-d" ] ; then
		i=$((i+1))
		dataset=${args[${i}]}

	elif [ ${args[${i}]} = "-r" ] ; then
		i=$((i+1))
		treads=${args[${i}]}

	elif [ ${args[${i}]} = "-us" ] ; then
		i=$((i+1))
		userSuffix=${args[${i}]}

	elif [ ${args[${i}]} = "-is" ] ; then
		i=$((i+1))
		itemSuffix=${args[${i}]}

	elif [ ${args[${i}]} = "-s" ] ; then
		i=$((i+1))
		userSuffix=${args[${i}]}
		itemSuffix=${args[${i}]}

	elif [ ${args[${i}]} = "-c" ] ; then
		cont="c"
		

	else
		echo "Unknown option."
		echo "Possible options are:"
		echo "-a <algorithm>"
		echo "-t <hh:mm:ss>"
		echo "-n <nrnodes>"
		echo "-k <K>"
		echo "-d <dataset>"
		echo "-r <treads>"
		echo ""	
		echo "Possible algorithms are:"
		echo "AUM"
		echo "AUS"
		echo "AIS"
		echo "AIM"
		echo "AAAM"
		echo "APTS"
		echo ""
		echo "Possible datasets are:"
		echo "tes"
		echo "val"
		exit 0
	fi

done

#Caculate remaining parameters
blocks=$(($stop/$k))
blocksPerNode=$(($blocks/$nrnodes))
batch=$(($k*$blocksPerNode))
batchStart=$start

#Print parameters
echo "Algorithm       : $algorithm" 
echo "Start           : $start"
echo "Stop            : $stop"
echo "Number of Nodes : $nrnodes"
echo "Time            : $time"
echo "K               : $k"
echo "Number of Blocks: $blocks"
echo "Blocks per Node : $blocksPerNode"
echo "Batch size      : $batch"
echo ""

#Check input
if [ $batch -lt 1 ] ; then
	echo "Batch size to small. Consider allocating less nodes"
	exit 1
fi


#Launch the program
counter=1
batchStop=0

while [ $batchStop -lt $stop ]
do
	batchStop=$(($batchStart + $batch))
		
	if [ $(($batchStop+$batch)) -gt $stop ];	then	
		batchStop=$stop
	fi
	
	echo "Node: $counter Gets: $batchStart $batchStop (Size: $(($batchStop-$batchStart)))"
	
	prun -t $time -np 1 java -Xmx22g -ss1g ScheduleAll $algorithm $dataset $k $batchStart $batchStop $treads $cont $userSuffix $itemSuffix < /dev/null > output${counter}.txt &
	batchStart=$(($batchStart + $batch))
	counter=$(($counter + 1))
done


